package com.by.zhdoctor.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.by.zhdoctor.domain.Doctortest;

import com.by.zhdoctor.repository.DoctortestRepository;
import com.by.zhdoctor.web.rest.util.HeaderUtil;
import com.by.zhdoctor.web.rest.util.PaginationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Doctortest.
 */
@RestController
@RequestMapping("/api")
public class DoctortestResource {

    private final Logger log = LoggerFactory.getLogger(DoctortestResource.class);
        
    @Inject
    private DoctortestRepository doctortestRepository;

    /**
     * POST  /doctortests : Create a new doctortest.
     *
     * @param doctortest the doctortest to create
     * @return the ResponseEntity with status 201 (Created) and with body the new doctortest, or with status 400 (Bad Request) if the doctortest has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/doctortests")
    @Timed
    public ResponseEntity<Doctortest> createDoctortest(@RequestBody Doctortest doctortest) throws URISyntaxException {
        log.debug("REST request to save Doctortest : {}", doctortest);
        if (doctortest.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("doctortest", "idexists", "A new doctortest cannot already have an ID")).body(null);
        }
        Doctortest result = doctortestRepository.save(doctortest);
        return ResponseEntity.created(new URI("/api/doctortests/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("doctortest", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /doctortests : Updates an existing doctortest.
     *
     * @param doctortest the doctortest to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated doctortest,
     * or with status 400 (Bad Request) if the doctortest is not valid,
     * or with status 500 (Internal Server Error) if the doctortest couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/doctortests")
    @Timed
    public ResponseEntity<Doctortest> updateDoctortest(@RequestBody Doctortest doctortest) throws URISyntaxException {
        log.debug("REST request to update Doctortest : {}", doctortest);
        if (doctortest.getId() == null) {
            return createDoctortest(doctortest);
        }
        Doctortest result = doctortestRepository.save(doctortest);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("doctortest", doctortest.getId().toString()))
            .body(result);
    }

    /**
     * GET  /doctortests : get all the doctortests.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of doctortests in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/doctortests")
    @Timed
    public ResponseEntity<List<Doctortest>> getAllDoctortests(Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of Doctortests");
        Page<Doctortest> page = doctortestRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/doctortests");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /doctortests/:id : get the "id" doctortest.
     *
     * @param id the id of the doctortest to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the doctortest, or with status 404 (Not Found)
     */
    @GetMapping("/doctortests/{id}")
    @Timed
    public ResponseEntity<Doctortest> getDoctortest(@PathVariable Long id) {
        log.debug("REST request to get Doctortest : {}", id);
        Doctortest doctortest = doctortestRepository.findOne(id);
        return Optional.ofNullable(doctortest)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /doctortests/:id : delete the "id" doctortest.
     *
     * @param id the id of the doctortest to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/doctortests/{id}")
    @Timed
    public ResponseEntity<Void> deleteDoctortest(@PathVariable Long id) {
        log.debug("REST request to delete Doctortest : {}", id);
        doctortestRepository.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("doctortest", id.toString())).build();
    }

}
